import { _decorator, Component, Node, input, EventKeyboard, Input, KeyCode, Vec3, EventTouch, Vec2, Camera, Vec4, UITransform, director } from 'cc';
import { GameManager } from './GameManager';
import { UIManager } from './UIManager';
const { ccclass, property } = _decorator;

@ccclass('JoyStick')
export class JoyStick extends Component
{
    public static PLAYER_MOVE_SPEED: number = 3

    @property({ type: Node })
    public Player: Node | null = null
    @property({ type: Node })
    public JoyStickNode: Node | null = null
    @property({ type: Node })
    public JoyStickCenter: Node | null = null

    private curTouchPos: Vec2 = new Vec2()
    private lastTouchPos: Vec2 = new Vec2()
    private moveNormal: Vec2 = new Vec2()
    private curPlayerPos: Vec3 = new Vec3()
    private curJoyStickCenterPos: Vec2 = new Vec2()
    private isTouch: boolean = false
    private border:Vec4 = new Vec4()

    start()
    {
        input.on(Input.EventType.TOUCH_MOVE, this.OnTouchMove, this)
        input.on(Input.EventType.TOUCH_END, this.OnTouchEnd, this)
        input.on(Input.EventType.TOUCH_START, this.OnTouchStart, this)
        this.JoyStickNode.active = false
        let borders = GameManager.instance.Border.children
        this.border.x = borders[0].getPosition().y // 上
        this.border.y = borders[1].getPosition().y // 下
        this.border.z = borders[2].getPosition().x // 左
        this.border.w = borders[3].getPosition().x // 右
    }

    update(deltaTime: number)
    {
        if (GameManager.instance.IsGamePause > 0) {
            return
        }
        if (this.isTouch && this.Player != null)
        {
            this.Player.getPosition(this.curPlayerPos)
            this.curPlayerPos.x += this.moveNormal.x * JoyStick.PLAYER_MOVE_SPEED
            this.curPlayerPos.y += this.moveNormal.y * JoyStick.PLAYER_MOVE_SPEED
            this.curPlayerPos.z = 0
            // todo 封成一个方法
            this.curPlayerPos.y = Math.min(Math.max(this.curPlayerPos.y,this.border.y),this.border.x)
            this.curPlayerPos.x = Math.min(Math.max(this.curPlayerPos.x,this.border.z),this.border.w)
            this.Player.setPosition(this.curPlayerPos)
            
            if (this.moveNormal.x > 0) { this.Player.setScale(new Vec3(-1, 1, 1)) }
            else{ this.Player.setScale(new Vec3(1, 1, 1)) }
            
            GameManager.instance.PlayerController.SetIsMoving(true)
        }
        else
        {
            GameManager.instance.PlayerController.SetIsMoving(false)
        }
    }

    OnTouchStart(event: EventTouch)
    {
        event.getLocation(this.lastTouchPos)
        this.isTouch = true

        this.JoyStickNode.active = true
        
        let uiPos = UIManager.instance.TouchPosToUIPos(this.lastTouchPos)
        this.node.setPosition(uiPos.x, uiPos.y)
    }

    OnTouchEnd(event: EventTouch)
    {
        this.isTouch = false
        this.JoyStickNode.active = false
    }

    OnTouchMove(event: EventTouch)
    {
        event.getLocation(this.curTouchPos)
        Vec2.subtract(this.moveNormal, this.curTouchPos, this.lastTouchPos)
        this.moveNormal = this.moveNormal.normalize()
        let offset = new Vec2()
        Vec2.multiplyScalar(offset,this.moveNormal, 40)
        Vec2.add(this.curJoyStickCenterPos, Vec2.ZERO, offset)
        this.JoyStickCenter.setPosition(this.curJoyStickCenterPos.x, this.curJoyStickCenterPos.y)
    }
    
}


